﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.hud.components {
    import flash.events.*;
    import flash.display.*;
    import Display.*;
    import Modules.quest.Display.QuestManager.*;
    import Init.*;
    import Display.hud.*;

    public class HUDQuestManagerComponent extends HUDQuestBarComponent {

        public static const MAX_QUEST_SIDEBAR_SLOTS:int = 4;
        public static const QUEST_MANAGER_NAME:String = "QuestManager";

        protected var m_loadedQuestIcons:Array;

        public function HUDQuestManagerComponent(){
            this.m_loadedQuestIcons = new Array();
            UI.questManagerView.addEventListener(QuestManagerView.QUEST_MANAGER_ENABLED, this.startQuestManagerMode, false, 0, true);
            UI.questManagerView.addEventListener(QuestManagerView.QUEST_MANAGER_DISABLED, this.endQuestManagerMode, false, 0, true);
        }
        override public function cleanUp():void{
            super.cleanUp();
            UI.questManagerView.removeEventListener(QuestManagerView.QUEST_MANAGER_ENABLED, this.startQuestManagerMode);
            UI.questManagerView.removeEventListener(QuestManagerView.QUEST_MANAGER_DISABLED, this.endQuestManagerMode);
        }
        override public function addIcon(_arg1:String, _arg2:String, _arg3:DisplayObject, _arg4:Function, _arg5:int=0):Boolean{
            if (((_arg1) && (m_icons.hasOwnProperty(_arg1)))){
                return (false);
            };
            if (((_arg1) && (UI.questManagerView.content.iconExists(_arg1)))){
                return (false);
            };
            this.storeQuestIcon(_arg1, _arg2, _arg3, _arg4);
            if (Global.player.numQuestsLoading > 0){
                return (false);
            };
            var _local6:Boolean = this.processStoredQuests();
            if (_local6){
                Global.player.saveQuestOrderSequence();
            };
            return (_local6);
        }
        override public function removeIcon(_arg1:String, _arg2:Function, _arg3:Boolean=true):DisplayObject{
            var _local5:XML;
            var _local6:int;
            var _local7:Boolean;
            var _local8:XML;
            var _local9:XML;
            var _local4:DisplayObject = super.removeIcon(_arg1, _arg2, _arg3);
            if (!_local4){
                UI.questManagerView.content.removeIconByName(_arg1);
            } else {
                if (_arg3){
                    _local5 = QuestSettingsInit.getQuestXMLByName(_arg1);
                    _local6 = Global.gameSettings.getInt("maxQuestNumWithManager", 14);
                    if ((_local5.sequels as XMLList).length() <= 0){
                        this.addFirstQuestFromQuestManager();
                    } else {
                        if (((((_local5.sequels as XMLList).length() > 0)) && ((Global.questManager.getActiveQuests().length >= _local6)))){
                            _local7 = false;
                            for each (_local8 in _local5.sequels.sequel) {
                                if (_local8.attribute("name").length() <= 0){
                                } else {
                                    _local9 = QuestSettingsInit.getQuestXMLByName(_local8.attribute("name"));
                                    if (!_local9){
                                    } else {
                                        if (_local9.attribute("forcePriority") == "true"){
                                            _local7 = true;
                                            break;
                                        };
                                    };
                                };
                            };
                            if (!_local7){
                                this.addFirstQuestFromQuestManager();
                            };
                        };
                    };
                };
            };
            return (_local4);
        }
        protected function storeQuestIcon(_arg1:String, _arg2:String, _arg3:DisplayObject, _arg4:Function):void{
            var _local5:QuestManagerSprite = new QuestManagerSprite(_arg1, _arg2, _arg3, _arg4);
            this.m_loadedQuestIcons.push(_local5);
        }
        protected function addToQuestManager(_arg1:QuestManagerSprite):void{
            UI.questManagerView.content.addIcon(_arg1);
        }
        public function addQuestManagerSprite(_arg1:QuestManagerSprite):Boolean{
            if (_arg1){
                return (super.addIcon(_arg1.name, _arg1.questToolTip, _arg1.questIcon, _arg1.questCallBack));
            };
            return (false);
        }
        public function removeLastIconAndAddToQuestManager():Boolean{
            var _local4:DisplayObject;
            var _local5:QuestManagerSprite;
            var _local1:String = m_questNames[0];
            if (!m_icons[_local1]){
                return (false);
            };
            var _local2:Function = m_icons[_local1].clickCallback;
            var _local3:GameSprite = (this.removeIcon(_local1, _local2, false) as GameSprite);
            if (((_local3) && ((_local3.numChildren > 0)))){
                _local4 = _local3.getChildAt((_local3.numChildren - 1));
                _local5 = new QuestManagerSprite(_local3.name, _local3.getToolTipStatus(), _local4, _local2);
                UI.questManagerView.content.addIcon(_local5);
                return (true);
            };
            return (false);
        }
        protected function addFirstQuestFromQuestManager():Boolean{
            var _local1:QuestManagerSprite = UI.questManagerView.content.popFirstQuestIcon();
            if (_local1){
                return (this.addQuestManagerSprite(_local1));
            };
            return (false);
        }
        protected function processStoredQuests():Boolean{
            var _local5:QuestManagerSprite;
            var _local6:int;
            var _local7:int;
            var _local1:Array = new Array();
            var _local2:Array = new Array();
            var _local3:Array = new Array();
            var _local4:Boolean;
            for each (_local5 in this.m_loadedQuestIcons) {
                _local7 = Global.player.getSavedQuestSlot(_local5.name);
                if (_local7 >= 0){
                    _local1[_local7] = _local5;
                } else {
                    if (Global.player.wasInQuestManager(_local5.name)){
                        _local2.push(_local5);
                    } else {
                        _local3.push(_local5);
                    };
                };
            };
            this.m_loadedQuestIcons.splice(0, this.m_loadedQuestIcons.length);
            _local6 = m_questNames.length;
            for each (_local5 in _local1) {
                if (_local6 >= MAX_QUEST_SIDEBAR_SLOTS){
                    _local4 = this.removeLastIconAndAddToQuestManager();
                    if (!_local4){
                        return (false);
                    };
                };
                _local4 = this.addQuestManagerSprite(_local5);
                if (!_local4){
                    return (false);
                };
                _local6++;
            };
            for each (_local5 in _local3) {
                if (_local6 >= MAX_QUEST_SIDEBAR_SLOTS){
                    _local4 = this.removeLastIconAndAddToQuestManager();
                    if (!_local4){
                        return (false);
                    };
                };
                _local4 = this.addQuestManagerSprite(_local5);
                if (!_local4){
                    return (false);
                };
                _local6++;
            };
            for each (_local5 in _local2) {
                if (_local6 < MAX_QUEST_SIDEBAR_SLOTS){
                    _local4 = this.addQuestManagerSprite(_local5);
                    if (!_local4){
                        return (false);
                    };
                    _local6++;
                } else {
                    this.addToQuestManager(_local5);
                };
            };
            return (_local4);
        }
        public function updateQuestUI():Boolean{
            return (this.processStoredQuests());
        }
        public function startQuestManagerMode(_arg1:Event):void{
            Global.hud.attachComponentToUI(HUD.COMP_QUESTS);
        }
        public function endQuestManagerMode(_arg1:Event):void{
            Global.hud.attachComponentBackToHUD(HUD.COMP_QUESTS);
        }

    }
}//package Display.hud.components 
